#include "shm.h"


int commShm(int flags, int sz)
{
    key_t key = ftok(PATH, ID);
    if(key < 0)
    {
        perror("key fail");
        exit(1);
    }

    int idcode = shmget(key, sz, flags);
    if(idcode < 0)
    {
        perror("shmget fail");
        exit(2);
    }

    return idcode;
}

int createShm(int sz)
{
    return commShm(IPC_CREAT | IPC_EXCL | 0666, sz);
}

int getShm(int sz)
{
    return commShm(IPC_CREAT, sz);
}

void destroyShm(int shmid)
{
    if(shmctl(shmid, IPC_RMID, nullptr) < 0)
    {
        perror("destory fail");
        exit(3);
    }
}